package func.study.jvm.c1;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * 方法区（永久代）内存溢出
 * 注：此测试只在jdk7及以下有效，jdk8移除了永久代，添加了Metaspace内存区域
 * VM args:
 * jdk7: -XX:PermSize=10M -XX:MaxPermsize=10M
 * jdk8: -XX:MaxMetaspaceSize=10M
 *
 * Created by chenyao on 2017/7/7.
 */
public class JavaMethodAreaOOM {
    public static void main(final String[] args) {
        long i = 0;
        while(true) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(OOMOjbect.class);
            enhancer.setUseCache(false);
            enhancer.setCallback(new MethodInterceptor() {
                @Override
                public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                    return methodProxy.invokeSuper(o, args);
                }
            });
            enhancer.create();
            System.out.println(i++);
        }
    }


    static class OOMOjbect {

    }






}
